package com.pshcp.patient.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.common.config.CustomConf;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.tool.PhotoTools;
import com.pshcp.domain.dto.PatientOnlineMedicalRecordsDTO;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientOnlineMedicalRecords;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.domain.model.PatientUser;
import com.pshcp.patient.domain.onlinemedical.ao.OnlineMedicalDetailAO;
import com.pshcp.patient.domain.onlinemedical.ao.OnlineMedicalDetailByOnlineOrderAO;
import com.pshcp.patient.domain.onlinemedical.vo.OnlineMedicalVO;
import com.pshcp.patient.domain.onlineorder.ao.OnlineListAO;
import com.pshcp.patient.tool.OnlineMedicalTools;
import com.pshcp.patient.tool.PatientTools;
import com.pshcp.service.PatientOnlineMedicalRecordsService;
import com.pshcp.service.PatientOnlineOrderService;
import com.pshcp.service.PatientService;
import com.pshcp.service.PatientUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = {"患者端-在线咨询病情描述"})
@Slf4j
@RestController
@RequestMapping("/api/patient/onlineOrderMedical")
public class OnlineOrderMedicalAPI {
    @Autowired
    private CustomConf customConf;

    @Autowired
    private PatientOnlineMedicalRecordsService medicalRecordsService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private PatientUserService patientUserService;

    @ApiOperation("病情描述详情(根据咨询单ID查询)")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "病情描述详情(根据咨询单ID查询)")
    @PostMapping("/medicalDetailByOnlineOrderId")
    public ResultDTO<OnlineMedicalVO> medicalDetailByOnlineOrderId(@Valid @RequestBody OnlineMedicalDetailByOnlineOrderAO detailAO) {
        PatientOnlineMedicalRecords medicalRecords = medicalRecordsService.findByOnlineOrderId(detailAO.getOrderId());
        OnlineMedicalVO vo = OnlineMedicalTools.buildModel2VO(medicalRecords);

        Patient patient = patientService.findById(vo.getPatientId());
        vo.setAge(PatientTools.getAge(patient.getIdCardNo()));
        vo.setSex(PatientTools.getSexDesc(patient.getIdCardNo()));
        vo.setPhone(patient.getPhone());
        vo.setPatientCardNo(patient.getPatientCardNo());

        PatientUser patientUser = patientUserService.findById(medicalRecords.getPatientUserId());
        vo.setFullHeadPhotoPath(PhotoTools.getFullPath(customConf.getFileUrl(), patientUser.getHeadPhoto()));
        return ResultBuilder.success(vo);
    }

    @ApiOperation("病情描述详情")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "病情描述详情")
    @PostMapping("/medicalDetail")
    public ResultDTO<OnlineMedicalVO> medicalDetail(@Valid @RequestBody OnlineMedicalDetailAO medicalDetailAO) {
        PatientOnlineMedicalRecords medicalRecords = medicalRecordsService.findById(medicalDetailAO.getOrderMedicalId());
        OnlineMedicalVO vo = OnlineMedicalTools.buildModel2VO(medicalRecords);

        Patient patient = patientService.findById(vo.getPatientId());
        vo.setAge(PatientTools.getAge(patient.getIdCardNo()));
        vo.setSex(PatientTools.getSexDesc(patient.getIdCardNo()));
        vo.setPhone(patient.getPhone());
        vo.setPatientCardNo(patient.getPatientCardNo());

        PatientUser patientUser = patientUserService.findById(medicalRecords.getPatientUserId());
        vo.setFullHeadPhotoPath(PhotoTools.getFullPath(customConf.getFileUrl(), patientUser.getHeadPhoto()));
        return ResultBuilder.success(vo);
    }

    @ApiOperation("病情描述列表")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "病情描述列表")
    @PostMapping("/medicalList")
    public ResultDTO<PageList<OnlineMedicalVO>> medicalList(@RequestBody OnlineListAO onlineListAO) {
        PatientOnlineMedicalRecordsDTO recordsDTO = OnlineMedicalTools.buildListAO2DTO(onlineListAO);
        recordsDTO.setPatientUserId(ReqComHold.getReqCom().getUserId());
        PageList<PatientOnlineMedicalRecords> pageList = medicalRecordsService.list(recordsDTO);
        if(pageList == null || CollectionUtils.isEmpty(pageList.getItems())){
            return ResultBuilder.success();
        }
        List<OnlineMedicalVO> medicalVOS = OnlineMedicalTools.buildListModel2VO(pageList.getItems());
        PageList<OnlineMedicalVO> medicalVOPageList = CommonTool.buildPageList(pageList, OnlineMedicalVO.class);
        medicalVOPageList.setItems(medicalVOS);
        //set patient info
        List<Long> patientIds = medicalVOS.stream().map(mv -> mv.getPatientId()).collect(Collectors.toList());
        List<Patient> patients = patientService.findByIds(patientIds);
        setPatientInfo(medicalVOS, patients);
        return ResultBuilder.success(medicalVOPageList);
    }

    private void setPatientInfo(List<OnlineMedicalVO> medicalVOS, List<Patient> patients){
        Map<Long, Patient> idAndPatientMap = patients.stream().collect(Collectors.toMap(Patient::getId, p -> p));
        medicalVOS.forEach(m -> {
            Patient patient = idAndPatientMap.get(m.getPatientId());
            m.setPhone(patient.getPhone());
            m.setPatientCardNo(patient.getPatientCardNo());
            m.setSex(PatientTools.getSexDesc(patient.getIdCardNo()));
            m.setAge(PatientTools.getAge(patient.getIdCardNo()));
        });
    }

}
